import { utils } from "../../../../utils/CommonUtil";
import RotateObj from "./RotateObj";
import GamePropObj from "./GamePropObj";

export default class GamePropObjPrefabRoot extends Laya.Script3D{

    private book_02_props:Laya.Sprite3D;
    private book_02_propsRotation : Laya.Vector3;
    private Round_key :Laya.Sprite3D;

    private key_01_props :Laya.Sprite3D;
    
    private GameObject :Laya.Sprite3D;
    private GameObjectRotation :Laya.Vector3;
    private lighter_props1 :Laya.Sprite3D;

    private RotateCameraCache:Laya.Sprite3D;

    public GetRotateCameraCache():Laya.Sprite3D{
        if(this.RotateCameraCache){
            this.RotateCameraCache.active = true;
        }
        return this.RotateCameraCache;
    }

    public SetRotateCameraCache(rotateCamera:Laya.Sprite3D){
        rotateCamera.active = false;
        this.RotateCameraCache = rotateCamera;
    }

    //重置prefab
    public ResetGamePropObjPrefab(){
        let childCount = this.owner.numChildren;
        for (let index = 0; index < childCount; index++) {
            let child = this.owner.getChildAt(index);
            child.active = false;
        }

        this.book_02_props.transform.localRotationEuler = this.book_02_propsRotation.clone();
        (this.book_02_props.getComponent(Laya.PhysicsCollider) as Laya.PhysicsCollider).enabled = true
        this.Round_key.active = true;

        this.key_01_props.active = true;

        this.GameObject.transform.localRotationEuler = this.GameObjectRotation.clone();
        (this.GameObject.getComponent(Laya.PhysicsCollider) as Laya.PhysicsCollider).enabled = true
        this.lighter_props1.active = true;
    }

    //初始化可交互的物体的组件
    public InitGamePropObjPrefabComponents(){
        let childCount = this.owner.numChildren;
        for (let index = 0; index < childCount; index++) {
            let child = this.owner.getChildAt(index);
            let childName = child.name;
            switch (childName){
                case "book_spark_flug_prop":
                {
                    let rotateNode = this.book_02_props =child.getChildAt(0).getChildAt(1).getChildByName("book_02_props") as Laya.Sprite3D;
                    this.book_02_propsRotation = rotateNode.transform.localRotationEuler.clone();
                    let rotatrObj = utils.CommonUtils.CustomAddComponent(rotateNode,RotateObj) as RotateObj;
                    rotatrObj.rotateTime = 500;
                    rotatrObj.IsRotateOnce = true;
                    rotatrObj.targetRotate = new Laya.Vector3(0,-50,0);
                    rotatrObj.isConvertUnityRotation = true;
                    let gamePropObjNode = this.Round_key =child.getChildAt(0).getChildByName("Round key") as Laya.Sprite3D;
                    let gamePropObj = utils.CommonUtils.CustomAddComponent(gamePropObjNode,GamePropObj) as GamePropObj;
                    gamePropObj.isNeedCallToLevel = false;
                    gamePropObj.gamePropId = 34;
                    break;
                }
                //热披萨，加入后能看到荧光字，需要修改Shader
                case "hotPiza_prop":
                {

                    // let SM_Item_Pizza_mima = child.getChildByName("SM_Item_Pizza_mima");
                    // console.log("第10关热披萨没做");
                    break;
                }
                case "Lamp_props":
                {
                    let gamePropObjNode = this.key_01_props = child.getChildByName("key_01_props") as Laya.Sprite3D;
                    let gamePropObj = utils.CommonUtils.CustomAddComponent(gamePropObjNode,GamePropObj) as GamePropObj;
                    gamePropObj.isNeedCallToLevel = false;
                    gamePropObj.gamePropId = 12;
                    break;
                }
                case "redBoxLighter":
                {
                    let rotateNode = this.GameObject = child.getChildByName("GameObject") as Laya.Sprite3D;
                    this.GameObjectRotation = rotateNode.transform.localRotationEuler.clone();
                    let rotatrObj = utils.CommonUtils.CustomAddComponent(rotateNode,RotateObj) as RotateObj;
                    rotatrObj.rotateTime = 500;
                    rotatrObj.IsRotateOnce = true;
                    rotatrObj.targetRotate = new Laya.Vector3(-80,0,0);

                    let gamePropObjNode =this.lighter_props1 = child.getChildByName("lighter_props (1)") as Laya.Sprite3D;
                    let gamePropObj = utils.CommonUtils.CustomAddComponent(gamePropObjNode,GamePropObj) as GamePropObj;
                    gamePropObj.isNeedCallToLevel = false;
                    gamePropObj.gamePropId = 6;
                    break;
                }
            } 
        }
    }

    public ShowGamePropObjByRenderPath(renderPath: string): Laya.Sprite3D {
        let renderPaths = renderPath.split("/");
        if (renderPaths.length != 3) {
            return;
        }

        let name = renderPaths[2];

        let childCount = this.owner.numChildren;
        let targetChild = null;
        for (let index = 0; index < childCount; index++) {
            let child = this.owner.getChildAt(index);
            if (child.active) {
                child.active = false;
            }
            if (child.name == name) {
                child.active = true;
                targetChild = child;
            }
        }

        return targetChild as Laya.Sprite3D;
    }

    public GetGameProObjByRenderPath(renderPath: string): Laya.Sprite3D{
        let renderPaths = renderPath.split("/");
        if (renderPaths.length != 3) {
            return;
        }
        let name = renderPaths[2];
        let childCount = this.owner.numChildren;
        for (let index = 0; index < childCount; index++) {
            let child = this.owner.getChildAt(index);
            if(child.name == name){
                return child as Laya.Sprite3D;
            }
        }
        return null;
    }
}